<template>
  <div style="border: black 0px solid;width: 100%;height: 550px;display: flex;
flex-direction: column;
align-items: center;">
    <!--    按钮栏    -->
    <div class="awsCenter" style="width: 100%;height: 40px;border: black 0px solid">
      <el-input style="width: 150px;margin-right: 10px" placeholder="船号" v-model="coupleSelectForm.ship_number"></el-input>
      <el-input style="width: 150px;margin-right: 10px" placeholder="船型" v-model="coupleSelectForm.ship_type"></el-input>

      <el-button @click="getCoupleList" style="width: 80px;margin-left: 10px" type="primary" plain>查询</el-button>
      <el-button @click="bayRefresh" style="margin-left: 10px" type="primary" circle><el-icon><refresh /></el-icon></el-button>
      <div style="margin-left: auto">
<!--        <el-button style="width: 80px;" type="primary" @click="showBay=true;bayFlag='new';bayTitle='新增调度信息';cleanBayForm();">新增</el-button>
        <el-button style="margin-left: 0px" @click="confDelete">批量删除</el-button>-->
        <ImportExcelButton style="margin-left: 20px" :fields="importFields" @import-data="coupleImportData"  />
      </div>

    </div>

    <!--   表格    -->
    <el-table :data="coupleList" stripe style="width: 100%;margin-top: 20px" @selection-change="selectClient">
      <el-table-column type="selection" :selectable="selectable" width="55" />
      <el-table-column prop="ship_number" label="船号" width="150" />
      <el-table-column prop="ship_type" label="船型" width="100" />
      <el-table-column prop="segment_number" label="分段号" width="100" />
      <el-table-column prop="segment_category" label="分段类型" width="100" />
      <el-table-column prop="station_cycle" label="胎位周期" width="100px"/>
      <el-table-column prop="segment_length" label="分段长（m）" width="150px"/>
      <el-table-column prop="segment_width" label="分段宽（m）" width="150px"/>
      <el-table-column prop="segment_height" label="分段高（m）" width="120px"/>
      <el-table-column prop="segment_weight" label="分段重量（t）" width="120px"/>
      <el-table-column prop="segment_production" label="分段产出" width="100" />
      <el-table-column label="组合匹配分段" width="120">
        <template #default="{ row }">
          <el-button @click="showCoupleData(row.coupleData);coupleTitle='组合匹配分段'" type="primary" text >点击查看</el-button>
        </template>
      </el-table-column>
<!--
      <el-table-column label="操作" width="100">
        <template #default="{ row }">
          <el-button type="primary" plain :icon="Edit" circle @click="showBay=true;bayFlag='edit';bayTitle='修改调度信息';showBayData(row)" />
          <el-button type="danger" plain :icon="Delete" circle @click="handleClientDelete(row)" />
        </template>
      </el-table-column>-->
    </el-table>
    <!-- 分页条 -->
    <div  class="demo-pagination-block">
      <el-pagination
        style="margin-top: 20px"
        v-model:current-page="currentPage"
        :page-size="8"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

  </div>


  <el-dialog
    v-model="showCouple"
    :title="coupleTitle"
    width="700px"
    align-center
    style="height: 580px;"
    draggable
    :close-on-click-modal="false"
  >
    <div class="detail-container">
      <div class="header-card">
        <h3>船舶 & 分段基本信息</h3>
        <p>详细信息展示</p>
      </div>

      <div class="info-card">
        <el-row :gutter="20" class="info-row">
          <el-col :span="12">
            <div class="label">分段编号</div>
            <div class="value">{{ coupleForm.segment_number || '--' }}</div>
          </el-col>
          <el-col :span="12">
            <div class="label">分段类别</div>
            <div class="value">{{ coupleForm.segment_category || '--' }}</div>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="info-row">
          <el-col :span="12">
            <div class="label">胎位周期</div>
            <div class="value">{{ coupleForm.station_cycle || '--' }}</div>
          </el-col>
          <el-col :span="12">
            <div class="label">是否组合段</div>
            <div class="value">{{ coupleForm.is_combined ? '是' : '否' }}</div>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="info-row">
          <el-col :span="12">
            <div class="label">分段长度 (m)</div>
            <div class="value">{{ coupleForm.segment_length || '--' }}</div>
          </el-col>
          <el-col :span="12">
            <div class="label">分段宽度 (m)</div>
            <div class="value">{{ coupleForm.segment_width || '--' }}</div>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="info-row">
          <el-col :span="12">
            <div class="label">分段高度 (m)</div>
            <div class="value">{{ coupleForm.segment_height || '--' }}</div>
          </el-col>
          <el-col :span="12">
            <div class="label">分段重量 (t)</div>
            <div class="value">{{ coupleForm.segment_weight || '--' }}</div>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="info-row">
          <el-col :span="12">
            <div class="label">生产状态</div>
            <div class="value">{{ coupleForm.segment_production || '--' }}</div>
          </el-col>
        </el-row>
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import {getUserClientService,userRegisterService,deleteClientService,updateClientService} from '@/api/user.js'
import {ElMessage, ElMessageBox, UploadFile} from "element-plus";
const confDialog = ref(false)
import moment from 'moment';
import type { ComponentSize } from 'element-plus'
const currentPage = ref(1)
const size = ref<ComponentSize>('default')
const background = ref(false)
const disabled = ref(false)
import { Refresh } from '@element-plus/icons-vue';

import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'
import {upload} from '@/api/upload.js'





const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = async (val: number) => {
  await getCoupleList(val)
}
import {
  Delete,
  Edit,
} from '@element-plus/icons-vue'
import {onMounted, ref, watch} from 'vue'
import type { TabsPaneContext } from 'element-plus'
const activeName = ref('size')

import {useUserStore} from "@/stores/user/user.js";
import { useRouter } from 'vue-router'
const userStore=useUserStore()
const router = useRouter();
onMounted(async ()=>{

  await getCoupleList(1)

})

const importFields = ref<string[]>([
  'ship_number',
  'segment_number',
  'segment_category',
  'segment_length',
  'segment_width',
  'segment_weight',
  'segment_production',
  'is_combined',
  'combined_segment'
])

//查询
const total=ref(0)



const selectable = (row, index) => {
  // 在这里可以设置某些条件下的行是否可选
  if (row.username===userStore.user.username){
    return false;
  }
  return true;
};






const selected=ref([])
function selectClient(selectedRows) {
  console.log(selectedRows)
  selected.value=selectedRows
}


const confDelete=(()=>{
  if (selected.value.length===0){
    ElMessage.warning("请先选择数据")
  }else {
    ElMessageBox.confirm(
      '您确定要删除吗？',
      '删除确认',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    ).then(() => {


      deleteBay()


    }).catch((err) => {
      console.log(err)
    });
  }
})

//单个删除
const handleClientDelete=(async (row)=>{
  selected.value.push(row)
  console.log(row)
  ElMessageBox.confirm(
    '您确定要删除吗？',
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {

    deleteBay()


  }).catch((err) => {
    console.log(err)
  });
  //confDialog.value=true
})

//公用 ：删除确认框
const confDeleteDialog=(()=>{

  confDialog.value = false
})



function formatDate(date) {
  if (!date) return '';

  const d = new Date(date);
  const year = d.getFullYear();
  const month = (d.getMonth() + 1).toString().padStart(2, '0');
  const day = d.getDate().toString().padStart(2, '0');

  return `${year}-${month}-${day}`;
}


import {getCoupleSegmentListService, importCoupleSegmentService} from '@/api/couple_segment.js'

//调度信息查询
import {
  addBayAssignedSegmentService,
  getBayAssignedSegmentsService,
  updateBayAssignedSegmentService,
  deleteBayAssignedSegmentService,
  batchDeleteBayAssignedSegmentsService, batchAddBayAssignedSegmentsService
} from '@/api/bay_assignment.js';
import ImportExcelButton from "@/components/ImportExcelButton.vue";


// 控制弹窗显示
const showCouple = ref(false);
const coupleFlag = ref('');  // 'new' 或 'edit'
const coupleTitle = ref('');

// 表单数据
const coupleForm = ref({
  ship_segments_id: '',
  ship_number: '',
  ship_type: '',
  segment_number: '',
  segment_category: '',
  station_cycle: '',
  segment_length: '',
  segment_schedule_id: '',
  segment_width: '',
  segment_height: '',
  segment_weight: '',
  segment_production: '',
  is_combined:'',
  combined_segment_id:''
});

// 清空表单
const cleanBayForm = () => {
  coupleForm.value = {
    ship_segments_id: '',
    ship_number: '',
    ship_type: '',
    segment_number: '',
    segment_category: '',
    station_cycle: '',
    segment_length: '',
    segment_schedule_id: '',
    segment_width: '',
    segment_height: '',
    segment_weight: '',
    segment_production: '',
    is_combined:'',
    combined_segment_id:''
  };
};

// 显示编辑数据
const showCoupleData = (row) => {
  coupleForm.value = { ...row };
  showCouple.value=true

};

// 查询条件表单
const coupleSelectForm = ref({
  current: 1,
  pageSize: 8,
  ship_number: '',
  ship_type: ''
});

// 分页数据
const coupleList = ref([]);


// 获取组合分段
const getCoupleList = async (val) => {
  coupleSelectForm.value.current = val;
  const res = await getCoupleSegmentListService(coupleSelectForm.value);
  if (res.code === 200) {
    coupleList.value = res.data.list;
    total.value = res.data.total;
  }
};


// 刷新查询
const bayRefresh = () => {
  coupleSelectForm.value.ship_number = '';
  coupleSelectForm.value.ship_type = '';
  getCoupleList(1);
};



const deleteBay = async () => {

  const res = await batchDeleteBayAssignedSegmentsService(selected.value);
  if (res.code === 200) {
    ElMessage.success(`成功删除 ${selected.value.length} 条记录`);
    await getCoupleList(1);
  }

  selected.value = [];
};

const images = ref([
  { date: '2025-06-01', url: '/imageData/2025-06-01.png' },
]);



const importedData = ref<any[]>([])

const coupleImportData = async (data: any[]) => {
  console.log('导入的数据:', data)
  importedData.value = data
  console.log('进来了')
  const res = await importCoupleSegmentService(importedData.value)
  if (res.code === 200) {
    ElMessage.success(res.message)
    await getCoupleList(1)
  }
}

</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 110px;
  height: 110px;
  text-align: center;
}
.demo-date-picker {
  display: flex;
  padding: 0;
  flex-wrap: wrap;
}
.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 0;
}
.demo-date-picker .block:last-child {
  border-right: none;
}
.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}

.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
.awsCenter{
  display: flex;
  align-items: center;
}


.detail-container {
  padding: 20px;
  font-family: "Helvetica Neue", sans-serif;
}

.header-card {
  background-color: #f5f7fa;
  border-radius: 8px;
  padding: 16px 20px;
  margin-bottom: 20px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.header-card h3 {
  margin: 0;
  font-size: 18px;
  color: #333;
}

.header-card p {
  margin-top: 4px;
  font-size: 13px;
  color: #666;
}

.info-card {
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.info-row {
  margin-bottom: 16px;
}

.label {
  font-size: 13px;
  color: #999;
  margin-bottom: 4px;
}

.value {
  padding: 10px;
  background-color: #f9f9f9;
  border-radius: 4px;
  font-size: 14px;
  color: #333;
  min-height: 32px;
}

.value.full-width {
  width: 100%;
}
</style>
